package org.example.ding.dto;

import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.example.ding.entity.User;

import java.math.BigDecimal;

/**
 * 用户注册数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserRegistrationDto {

    @NotBlank(message = "用户名不能为空")
    @Size(min = 3, max = 50, message = "用户名长度必须在3-50个字符之间")
    @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "用户名只能包含字母、数字和下划线")
    private String username;

    @NotBlank(message = "密码不能为空")
    @Size(min = 6, max = 20, message = "密码长度必须在6-20个字符之间")
    private String password;

    @NotBlank(message = "确认密码不能为空")
    private String confirmPassword;

    @NotBlank(message = "邮箱不能为空")
    @Email(message = "邮箱格式不正确")
    private String email;

    @Size(max = 50, message = "真实姓名长度不能超过50个字符")
    private String realName;

    @NotNull(message = "性别不能为空")
    private User.Gender gender;

    @NotNull(message = "年龄不能为空")
    @Min(value = 1, message = "年龄必须大于0")
    @Max(value = 150, message = "年龄不能超过150")
    private Integer age;

    @NotNull(message = "身高不能为空")
    @DecimalMin(value = "50.0", message = "身高必须大于50cm")
    @DecimalMax(value = "300.0", message = "身高不能超过300cm")
    private BigDecimal height;

    @NotNull(message = "体重不能为空")
    @DecimalMin(value = "20.0", message = "体重必须大于20kg")
    @DecimalMax(value = "1000.0", message = "体重不能超过1000kg")
    private BigDecimal weight;

    @NotNull(message = "活动水平不能为空")
    private User.ActivityLevel activityLevel;

    // 营养目标 (可选，系统会根据用户信息自动计算默认值)
    @DecimalMin(value = "800.0", message = "卡路里目标不能少于800")
    @DecimalMax(value = "5000.0", message = "卡路里目标不能超过5000")
    private BigDecimal dailyCaloriesGoal;

    @DecimalMin(value = "30.0", message = "蛋白质目标不能少于30g")
    @DecimalMax(value = "500.0", message = "蛋白质目标不能超过500g")
    private BigDecimal dailyProteinGoal;

    @DecimalMin(value = "50.0", message = "碳水化合物目标不能少于50g")
    @DecimalMax(value = "800.0", message = "碳水化合物目标不能超过800g")
    private BigDecimal dailyCarbsGoal;

    @DecimalMin(value = "20.0", message = "脂肪目标不能少于20g")
    @DecimalMax(value = "200.0", message = "脂肪目标不能超过200g")
    private BigDecimal dailyFatGoal;

    // 饮食限制和过敏信息
    private String dietaryRestrictions;
    private String allergies;

    /**
     * 验证密码确认
     */
    @AssertTrue(message = "两次输入的密码不一致")
    public boolean isPasswordMatching() {
        if (password == null || confirmPassword == null) {
            return false;
        }
        return password.equals(confirmPassword);
    }

    /**
     * 转换为User实体
     */
    public User toUser() {
        User user = new User();
        user.setUsername(this.username);
        user.setPassword(this.password); // 注意：密码需要在Service层加密
        user.setEmail(this.email);
        user.setRealName(this.realName);
        user.setGender(this.gender);
        user.setAge(this.age);
        user.setHeight(this.height);
        user.setWeight(this.weight);
        user.setActivityLevel(this.activityLevel);
        user.setDietaryRestrictions(this.dietaryRestrictions);
        user.setAllergies(this.allergies);
        
        // 设置营养目标，如果用户没有设置则使用计算的默认值
        if (this.dailyCaloriesGoal != null) {
            user.setDailyCaloriesGoal(this.dailyCaloriesGoal);
        }
        if (this.dailyProteinGoal != null) {
            user.setDailyProteinGoal(this.dailyProteinGoal);
        }
        if (this.dailyCarbsGoal != null) {
            user.setDailyCarbsGoal(this.dailyCarbsGoal);
        }
        if (this.dailyFatGoal != null) {
            user.setDailyFatGoal(this.dailyFatGoal);
        }
        
        return user;
    }

    /**
     * 计算推荐的营养目标
     */
    public void calculateRecommendedNutritionGoals() {
        if (height == null || weight == null || age == null || gender == null || activityLevel == null) {
            return;
        }

        // 计算TDEE作为卡路里目标
        double bmr;
        if (gender == User.Gender.MALE) {
            bmr = 10 * weight.doubleValue() + 6.25 * height.doubleValue() - 5 * age + 5;
        } else {
            bmr = 10 * weight.doubleValue() + 6.25 * height.doubleValue() - 5 * age - 161;
        }
        
        double tdee = bmr * activityLevel.getMultiplier();
        
        if (dailyCaloriesGoal == null) {
            this.dailyCaloriesGoal = BigDecimal.valueOf(tdee).setScale(0, BigDecimal.ROUND_HALF_UP);
        }
        
        // 蛋白质：体重 * 1.2-2.0g (取1.6g)
        if (dailyProteinGoal == null) {
            this.dailyProteinGoal = weight.multiply(new BigDecimal("1.6")).setScale(0, BigDecimal.ROUND_HALF_UP);
        }
        
        // 脂肪：总卡路里的25-30% (取27.5%)
        if (dailyFatGoal == null) {
            this.dailyFatGoal = dailyCaloriesGoal.multiply(new BigDecimal("0.275"))
                .divide(new BigDecimal("9"), 0, BigDecimal.ROUND_HALF_UP); // 脂肪9卡/克
        }
        
        // 碳水化合物：剩余卡路里
        if (dailyCarbsGoal == null) {
            BigDecimal proteinCalories = dailyProteinGoal.multiply(new BigDecimal("4"));
            BigDecimal fatCalories = dailyFatGoal.multiply(new BigDecimal("9"));
            BigDecimal remainingCalories = dailyCaloriesGoal.subtract(proteinCalories).subtract(fatCalories);
            this.dailyCarbsGoal = remainingCalories.divide(new BigDecimal("4"), 0, BigDecimal.ROUND_HALF_UP);
        }
    }
}
